#include "TcpServer.hpp"
#include "Protocol.hpp"
#include <memory>
#include "NetCal.hpp"
#include "Daemon.hpp"

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " port" << std::endl;
}

// ./tcpserver port
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = std::stoi(argv[1]);
    Enable_File_Log_Strategy(); // 将日志换成文件策略模式

    // 设置为守护进程
    Daemon(0, 0);

    // 1. 应用层业务
    std::unique_ptr<Cal> cal = std::make_unique<Cal>();

    // 2. 协议层
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>([&cal](Request &req) -> Response
                                                                    { return cal->Execute(req); });

    // 3. 服务器层
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(port, [&protocol](std::shared_ptr<Socket> &sock, InetAddr &client)
                                                                  { protocol->GetRequest(sock, client); });
    tsvr->Start();
    return 0;
}